<template>
    <div class="M-popup" v-if="config" v-show="config.visible">
        <div class="mask" @click="tapmask($event)"></div>
        <div class="M-popupBody">
            <div class="title" v-show="config.showHeader">
                {{title}}
                <span class="close" @click.stop="close($event)">x</span>
                </div>
            <div class="con">
                <slot></slot>
            </div>
            <div class="footer" v-show="config.showFooter" v-if="btns && btns.length">
                <button v-for="(btn,index) in btns" :key="index" @click="action(btn)" :data-type="btn.type" :disabled="btn.disable">{{btn.text}}</button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    props: {
        config: {
            type: Object,
            default: {
                visible: false,
                showHeader: true,
                maskTapable: true,
                showFooter: true
            }
        }
    },
    methods: {
        action: function(btn){
            if(btn.action){
                btn.action(btn);
            }
        },
        tapmask: function(e){
            //点击mask
            if(this.config.maskTapable){
                this.close(e);
            }
        },
        close: function(e){
            if(this.confg.visible) this.config.visible = false;
        }
    }
}
</script>

<style scoped lang="less">
.M-popup{
    position: absolute;
    z-index: 10;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    &Body{
        max-width: 80%;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
        font-size: 1rem;
        color: #000;
        .title{
            @th: 50/28rem;
            text-align: center;
            height: @th;
            line-height: @th;
            position: relative;
            .close{
                @cw: 20/28rem;
                width: @cw;
                height: @cw;
                line-height: @cw;
                text-align: center;
                border-radius: @cw*.5;
                color: #fff;
                background: rgba(0,0,0,.5);
                font-size: 18/28rem;
                position: absolute;
                right: 20/28rem;
                top: 20/28rem;
            }
        }
        .footer{
            @bh: 40/28rem;
            display: flex;
            justify-content: center;
            align-items: center;
            height: @bh;
            line-height: @bh;
            padding: 20/28rem;
            border-radius: @bh*.5;
            border: 1px solid #e1e1e1;
            &[data-type="ok"]{
                color: #fff;
                background: lightcoral;
            }
        }
    }
    .mask{
        width: 100%;
        height: 100%;
        background: rgba(0,0,0,.5);
    }
}
</style>